package com.firefly.admin.tripartite.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.firefly.admin.common.constant.Constants;
import com.firefly.admin.common.enums.ResultCodeEnum;
import com.firefly.admin.common.vo.Result;
import com.firefly.admin.tripartite.entity.FyTripartiteWechatOfficialAccount;
import com.firefly.admin.tripartite.mapper.FyTripartiteWechatOfficialAccountMapper;
import com.firefly.admin.tripartite.service.FyTripartiteWechatOfficialAccountService;
import com.firefly.admin.tripartite.vo.WechatOfficialAccountQueryVo;
import com.firefly.admin.tripartite.vo.WechatOfficialAccountStatusUpdateVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@Slf4j
public class FyTripartiteWechatOfficialAccountServiceImpl implements FyTripartiteWechatOfficialAccountService {

    @Autowired
    private FyTripartiteWechatOfficialAccountMapper fyTripartiteWechatOfficialAccountMapper;

    /**
     * 分页查询第三方微信公众号接口
     *
     * @param wechatOfficialAccountQueryVo 分页查询参数
     * @return 分页列表
     */
    @Override
    public Result<Page<FyTripartiteWechatOfficialAccount>> getWechatOfficialAccountList(WechatOfficialAccountQueryVo wechatOfficialAccountQueryVo) {
        log.info("###########FyTripartiteWechatOfficialAccountServiceImpl getWechatOfficialAccountList param = {}", JSONObject.toJSONString(wechatOfficialAccountQueryVo));
        LambdaQueryWrapper<FyTripartiteWechatOfficialAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(wechatOfficialAccountQueryVo.getOfficialAccountName()), FyTripartiteWechatOfficialAccount::getOfficialAccountName, wechatOfficialAccountQueryVo.getOfficialAccountName());
        queryWrapper.eq(StringUtils.isNotBlank(wechatOfficialAccountQueryVo.getStatus()), FyTripartiteWechatOfficialAccount::getStatus, wechatOfficialAccountQueryVo.getStatus());
        queryWrapper.eq(StringUtils.isNotBlank(wechatOfficialAccountQueryVo.getMenuStatus()), FyTripartiteWechatOfficialAccount::getMenuStatus, wechatOfficialAccountQueryVo.getMenuStatus());
        queryWrapper.eq(FyTripartiteWechatOfficialAccount::getDeleteFlag, Constants.DeleteFlag.NORMAL);
        queryWrapper.orderByDesc(FyTripartiteWechatOfficialAccount::getCreateTime);
        Page<FyTripartiteWechatOfficialAccount> page = new Page<>(wechatOfficialAccountQueryVo.getPage(), wechatOfficialAccountQueryVo.getLimit());
        Page<FyTripartiteWechatOfficialAccount> wechatOfficialAccountPage = fyTripartiteWechatOfficialAccountMapper.selectPage(page, queryWrapper);
        return Result.success(wechatOfficialAccountPage);
    }

    /**
     * 第三方微信公众号信息新增接口
     *
     * @param fyTripartiteWechatOfficialAccount 微信公众号信息
     * @return 操作结果
     */
    @Override
    public Result<FyTripartiteWechatOfficialAccount> add(FyTripartiteWechatOfficialAccount fyTripartiteWechatOfficialAccount) {
        if (fyTripartiteWechatOfficialAccount == null) {
            return Result.getResult(ResultCodeEnum.REQUEST_PARAM_EXCEPTION);
        }
        ResultCodeEnum resultCodeEnum = checkWechatOfficialAccountParam(fyTripartiteWechatOfficialAccount);
        if (resultCodeEnum != null) {
            return Result.getResult(resultCodeEnum);
        }
        String menuStatus = fyTripartiteWechatOfficialAccount.getMenuStatus();
        if (StringUtils.isBlank(menuStatus)) {
            fyTripartiteWechatOfficialAccount.setMenuStatus(Constants.OfficialAccountMenuStatus.UNPUBLISHED);
        }
        int rows = fyTripartiteWechatOfficialAccountMapper.insert(fyTripartiteWechatOfficialAccount);
        if (rows <= 0) {
            return Result.getResult(ResultCodeEnum.ADD_EXCEPTION);
        }
        return Result.success(fyTripartiteWechatOfficialAccount);
    }

    /**
     * 第三方微信公众号信息编辑接口
     *
     * @param fyTripartiteWechatOfficialAccount 微信公众号信息
     * @return 操作结果
     */
    @Override
    public Result<FyTripartiteWechatOfficialAccount> update(FyTripartiteWechatOfficialAccount fyTripartiteWechatOfficialAccount) {
        if (fyTripartiteWechatOfficialAccount == null) {
            return Result.getResult(ResultCodeEnum.REQUEST_PARAM_EXCEPTION);
        }
        Long officialAccountId = fyTripartiteWechatOfficialAccount.getOfficialAccountId();
        if (officialAccountId == null) {
            return Result.getResult(ResultCodeEnum.REQUEST_PARAM_EXCEPTION);
        }
        FyTripartiteWechatOfficialAccount wechatOfficialAccount = fyTripartiteWechatOfficialAccountMapper.selectById(officialAccountId);
        if (wechatOfficialAccount == null) {
            return Result.getResult(ResultCodeEnum.TRIPARTITE_WECHAT_OFFICIAL_ACCOUNT_NOT_FOUND);
        }
        ResultCodeEnum resultCodeEnum = checkWechatOfficialAccountParam(fyTripartiteWechatOfficialAccount);
        if (resultCodeEnum != null) {
            return Result.getResult(resultCodeEnum);
        }
        int rows = fyTripartiteWechatOfficialAccountMapper.updateById(fyTripartiteWechatOfficialAccount);
        if (rows <= 0) {
            return Result.getResult(ResultCodeEnum.UPDATE_EXCEPTION);
        }
        return Result.success(fyTripartiteWechatOfficialAccount);
    }

    /**
     * 第三方微信公众号状态编辑接口
     *
     * @param wechatOfficialAccountStatusUpdateVo 状态信息
     * @return 操作结果
     */
    @Override
    public Result<FyTripartiteWechatOfficialAccount> updateStatus(WechatOfficialAccountStatusUpdateVo wechatOfficialAccountStatusUpdateVo) {
        if (wechatOfficialAccountStatusUpdateVo == null) {
            return Result.getResult(ResultCodeEnum.REQUEST_PARAM_EXCEPTION);
        }
        Long officialAccountId = wechatOfficialAccountStatusUpdateVo.getOfficialAccountId();
        if (officialAccountId == null) {
            return Result.getResult(ResultCodeEnum.REQUEST_PARAM_EXCEPTION);
        }
        FyTripartiteWechatOfficialAccount fyTripartiteWechatOfficialAccount = fyTripartiteWechatOfficialAccountMapper.selectById(officialAccountId);
        if (fyTripartiteWechatOfficialAccount == null) {
            return Result.getResult(ResultCodeEnum.TRIPARTITE_WECHAT_OFFICIAL_ACCOUNT_NOT_FOUND);
        }
        String status = wechatOfficialAccountStatusUpdateVo.getStatus();
        if (StringUtils.isBlank(status)) {
            return Result.getResult(ResultCodeEnum.TRIPARTITE_WECHAT_OFFICIAL_ACCOUNT_STATUS_NOT_NULL);
        }
        fyTripartiteWechatOfficialAccount.setStatus(status);
        int rows = fyTripartiteWechatOfficialAccountMapper.updateById(fyTripartiteWechatOfficialAccount);
        if (rows <= 0) {
            return Result.getResult(ResultCodeEnum.UPDATE_EXCEPTION);
        }
        return Result.success(fyTripartiteWechatOfficialAccount);
    }

    /**
     * 第三方微信公众号信息删除接口
     *
     * @param officialAccountId 微信公众号ID
     * @return 操作结果
     */
    @Override
    public Result<String> delete(Long officialAccountId) {
        if (officialAccountId == null) {
            return Result.getResult(ResultCodeEnum.REQUEST_PARAM_EXCEPTION);
        }
        FyTripartiteWechatOfficialAccount fyTripartiteWechatOfficialAccount = fyTripartiteWechatOfficialAccountMapper.selectById(officialAccountId);
        if (fyTripartiteWechatOfficialAccount == null) {
            return Result.getResult(ResultCodeEnum.TRIPARTITE_WECHAT_OFFICIAL_ACCOUNT_NOT_FOUND);
        }
        fyTripartiteWechatOfficialAccount.setDeleteFlag(Constants.DeleteFlag.DELETE);
        int rows = fyTripartiteWechatOfficialAccountMapper.updateById(fyTripartiteWechatOfficialAccount);
        if (rows <= 0) {
            return Result.getResult(ResultCodeEnum.DELETE_EXCEPTION);
        }
        return Result.success();
    }

    /**
     * 第三方微信公众号APPID校验接口
     *
     * @param appId             微信公众号APPID
     * @param officialAccountId 微信公众号ID
     * @return 校验结果
     */
    @Override
    public Result<String> appIdCheck(String appId, Long officialAccountId) {
        if (StringUtils.isBlank(appId)) {
            return Result.getResult(ResultCodeEnum.REQUEST_PARAM_EXCEPTION);
        }
        boolean isExistAppId = isExistAppId(appId, officialAccountId);
        if (isExistAppId) {
            return Result.success(ResultCodeEnum.TRIPARTITE_WECHAT_OFFICIAL_ACCOUNT_APP_ID_EXIST.getMsg());
        }
        return Result.success();
    }

    public ResultCodeEnum checkWechatOfficialAccountParam(FyTripartiteWechatOfficialAccount fyTripartiteWechatOfficialAccount) {
        Long officialAccountId = fyTripartiteWechatOfficialAccount.getOfficialAccountId();
        String officialAccountName = fyTripartiteWechatOfficialAccount.getOfficialAccountName();
        if (StringUtils.isBlank(officialAccountName)) {
            return ResultCodeEnum.TRIPARTITE_WECHAT_OFFICIAL_ACCOUNT_NAME_NOT_NULL;
        }
        String appId = fyTripartiteWechatOfficialAccount.getAppId();
        if (StringUtils.isBlank(appId)) {
            return ResultCodeEnum.TRIPARTITE_WECHAT_APPLET_APPID_NOT_NULL;
        }
        boolean isExistAppId = isExistAppId(appId, officialAccountId);
        if (isExistAppId) {
            return ResultCodeEnum.TRIPARTITE_WECHAT_OFFICIAL_ACCOUNT_APP_ID_NOT_NULL;
        }
        String secretKey = fyTripartiteWechatOfficialAccount.getSecretKey();
        if (StringUtils.isBlank(secretKey)) {
            return ResultCodeEnum.TRIPARTITE_WECHAT_OFFICIAL_ACCOUNT_SECRET_KEY_NOT_NULL;
        }
        return null;
    }

    /**
     * 校验公众号APPID是否存在
     *
     * @param appId             公众号appId
     * @param officialAccountId 公众号ID
     * @return true: 存在   false:不存在
     */
    public boolean isExistAppId(String appId, Long officialAccountId) {
        LambdaQueryWrapper<FyTripartiteWechatOfficialAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FyTripartiteWechatOfficialAccount::getAppId, appId);
        queryWrapper.eq(FyTripartiteWechatOfficialAccount::getDeleteFlag, Constants.DeleteFlag.NORMAL);
        List<FyTripartiteWechatOfficialAccount> wechatAppletList = fyTripartiteWechatOfficialAccountMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(wechatAppletList)) {
            if (officialAccountId == null) {
                //新增
                return true;
            } else {
                //编辑
                for (FyTripartiteWechatOfficialAccount wechatOfficialAccount : wechatAppletList) {
                    if (officialAccountId.longValue() != wechatOfficialAccount.getOfficialAccountId().longValue()) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
}
